package com.example.owner.controller;

import com.example.owner.bean.RateLimit;
import com.example.owner.db.Owner;
import com.example.owner.repository.OwnerRepository;
import com.example.owner.service.OwnerService;
import io.micronaut.cache.annotation.Cacheable;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller("/owner")
public class OwnerController {

    @Inject
    private OwnerService ownerService;

    @Inject
    private RateLimit[] limits;

    @Get("/")
    @Produces(MediaType.TEXT_PLAIN)
    public String helloMicronaut() {
        for (RateLimit limit : limits) {
            log.info("limit is {}", limit);
        }
        return "Hello, Micronaut! 123123";
    }



    @Get("/add")
    @Produces(MediaType.TEXT_JSON)
    public Owner addOwner() {
        log.info("OwnerController >> addOwner owner");
        return getOwner();
    }

    @Cacheable("users")
    public Owner getOwner() {
        log.info("OwnerController >> getOwner owner");
        Owner owner = new Owner();
        owner.setFirstName("Foo");
        owner.setLastName("Bar");
        owner.setCity("Toronto");
        owner.setAddress("404 Adelaide St W");
        owner.setTelephone("647000999");
        Owner save = ownerService.save(owner);
        return save;
    }

}
